package com.wis.offline;

import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.*;

/**
 * @Description: traffic_wis
 * @author: fan
 * @Date: Created in 2019/10/21 12:12
 * @Modified By:
 */
public class ReadSrcFile {
    public static LinkedList<File> list = new LinkedList<>();

    public void checkNewFile(Properties properties, String input, String output) {
        list.clear();
        checkNewFile(new File(input));
        if (list.size() > 0) {
            File checkTWONewFilePath = list.getFirst();
            String existFile = checkExistFile(properties, checkTWONewFilePath.getName(), output);
            String[] split = existFile.split("_", 2);
            if (!Boolean.parseBoolean(split[0])) {// 不存在时
                readingFile(checkTWONewFilePath, new File(split[1]), properties);
            } else {
                System.out.println("最新文件已经存在：" + split[1]);
                return;
            }
        } else {
            System.out.println("没数据的路径：" + input);
        }
    }

    /**
     * 判断文件是否存在
     *
     * @param properties
     * @param fileName
     * @param output
     * @return
     */
    public String checkExistFile(Properties properties, String fileName, String output) {
        String date = fileName.split("_", -1)[6];
        String outputPath = output + "/" + date.substring(0, 4) + "/" + date.substring(0, 8) + "/" + fileName.replace("W", "R");
        System.out.println(outputPath);
        if (new File(outputPath).exists()) {
            outputPath = "true_" + outputPath;
        } else {
            outputPath = "false_" + outputPath;
        }
        return outputPath;
    }

    public void checkNewFile(File path) {
        if (path.exists()) {
            if (path.isDirectory()) {
                File[] files = path.listFiles(File::isDirectory);
                if (files.length > 0) {
                    List<File> linkedList = Arrays.asList(files);
                    Collections.sort(linkedList);
                    checkNewFile(linkedList.get(linkedList.size() - 1));
                } else {
                    files = path.listFiles(File::isFile);
                    List<File> linkedList = Arrays.asList(files);
                    Collections.sort(linkedList);
                    if (linkedList.size() > 0) {
                        linkedList.get(linkedList.size() - 1);
                        list.add(linkedList.get(linkedList.size() - 1));
                    }
                }
            }
        }
    }


    public void readingFile(File inputPath, File outputPath, Properties properties) {
        BufferedReader bufferedReader = null;
        BufferedWriter bufferedWriter = null;
        try {
            String fileName = inputPath.getName();
            if (fileName.contains("TWP")) {
                bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(inputPath), "GBK"));
            } else {
                bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(inputPath), "utf-8"));
            }
            if (!outputPath.getParentFile().exists()) {// 如果这个文件不存在
                boolean mkdirs = outputPath.getParentFile().mkdirs();
                if (!mkdirs) {
                    System.out.println("文件创建失败：" + outputPath.getParentFile());
                    return;
                }
            }

            bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputPath), "utf-8"));
            String str;
            int num = 0;
            String date = "";
            String typeProduce = "";
            int positionArr[] = new int[9];
            LinkedList<String> linkedList = new LinkedList<>();
            while ((str = bufferedReader.readLine()) != null) {
                String[] split = str.split(",", -1);
                if (num == 0) {
                    date = split[2];
                    typeProduce = split[0];
                    if (typeProduce.contains("预报")) {
                        typeProduce = "3";
                    }
                } else if (num == 1) {
                    if (split.length == 10) {
                        positionArr = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1};
                    } else if (split.length == 34) {
                        positionArr = new int[]{0, 1, 2, 6, 10, 14, 18, 22, 26, 30, 4};
                    } else {
                        positionArr = new int[]{0, 1, 2, 75, 148, 221, 294, 367, 440, 513, 73};
                    }
                } else {
                    str = OfflineTools.processDataByRow(split, date, positionArr, properties, typeProduce);
                    if (StringUtils.isNotBlank(str)) {
                        linkedList.add(str);
                    }
                }
                num++;
            }
            if (linkedList.size() > 0) {
                if (typeProduce.equals("1")) {
                    linkedList.add(0, "4," + linkedList.size() + "," + date + ",6,-1");
                    linkedList.add(1, Contant.troTitle);
                } else if (typeProduce.equals("2")) {
                    linkedList.add(0, "4," + linkedList.size() + "," + date + ",4,6,-1");
                    linkedList.add(1, Contant.trnTitle);
                } else if (typeProduce.equals("3")) {
                    linkedList.add(0, "4," + linkedList.size() + "," + date + "," + "73,6,-1");
                    linkedList.add(1, Contant.trpTitle);
                }

            }
            for (int i = 0; i < linkedList.size(); i++) {
                bufferedWriter.write(linkedList.get(i));
                bufferedWriter.newLine();
                bufferedWriter.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedWriter != null) bufferedWriter.close();
                if (bufferedReader != null) bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
